package com.caffeinatedbliss.nanowrimo.spike;

import com.caffeinatedbliss.nanowrimo.DynamicImageGenerator;

import java.awt.*;
import java.awt.image.BufferedImage;

public class Nanowidget implements DynamicImageGenerator {
  private static final int CURVE_RADIUS = 15;
  public static final int WIDTH = 140;
  public static final int HEIGHT = 204;

  public static final Color LIGHTEST_GREY = new Color(0xf0, 0xf0, 0xf0);
  public static final Color NOT_SO_LIGHTEST_GREY = new Color(0xbb, 0xbb, 0xdd);
  private static final GradientPaint GRADIENT = new GradientPaint(CURVE_RADIUS, CURVE_RADIUS,
    LIGHTEST_GREY, 2 * CURVE_RADIUS, CURVE_RADIUS * 7, NOT_SO_LIGHTEST_GREY, false);

  public static final Color GREY_BORDER = new Color(0xbb, 0xbb, 0xbb);
  public static final Color DK_GREY_BORDER = new Color(0x99, 0x99, 0x99);
  private static final GradientPaint OUTSIDE_GRADIENT = new GradientPaint(CURVE_RADIUS, CURVE_RADIUS,
    GREY_BORDER, 2 * CURVE_RADIUS, CURVE_RADIUS * 7, DK_GREY_BORDER, false);

  public static final Color GRAPH_BACKGROUND = new Color(0xee, 0xee, 0xee);
  public static final Color GREEN_INK = new Color(0x00, 0xcc, 0x00);

  private BufferedImage graphImage;

  public Nanowidget() {

    graphImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = (Graphics2D) graphImage.getGraphics();

    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    drawFilledRectangleWithCurvedCorners(g);

    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
    drawPercentCompleteBar(g);
    drawDetailedBarChart(g);

    g.dispose();
  }

  private void drawDetailedBarChart(Graphics2D g) {
    g.setColor(GREY_BORDER);
    g.drawRect(7, 43, WIDTH - 15, 152);
    g.setColor(GRAPH_BACKGROUND);
    g.fillRect(8, 44, WIDTH - 16, 151);
  }

  private void drawPercentCompleteBar(Graphics2D g) {
    g.setColor(GREY_BORDER);
    g.drawRect(5, 15, WIDTH - 11, 10);
    g.setColor(GRAPH_BACKGROUND);
    g.fillRect(6, 16, WIDTH - 13, 9);
  }

  private void drawFilledRectangleWithCurvedCorners(Graphics2D g) {
    g.setPaint(GRADIENT);
    g.fillRoundRect(0, 0, WIDTH - 1, HEIGHT - 1, 2 * CURVE_RADIUS, 2 * CURVE_RADIUS);

    g.setPaint(OUTSIDE_GRADIENT);
    g.drawRoundRect(0, 0, WIDTH - 1, HEIGHT - 1, 2 * CURVE_RADIUS, 2 * CURVE_RADIUS);
  }

  public BufferedImage getGraphImage() {
    return graphImage;
  }
}
